package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class LoginController{

    @Reference
    private IUserInfoService userInfoService;

    @ResponseBody
    @RequireLogin
    @RequestMapping("/hello")
    public Object get(Long id){
        return userInfoService.get(id);
    }

    @ResponseBody
    @RequestMapping("/checkPhone")
    public boolean checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    @ResponseBody
    @RequestMapping("/sendVerifyCode")
    public AjaxResult sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    @ResponseBody
    @RequestMapping("/userRegist")
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode){
        userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return AjaxResult.SUCCESS;
    }

    @ResponseBody
    @RequestMapping("/userLogin")
    public AjaxResult userLogin(HttpServletResponse response, String username, String password){
        // token 同时作为 cookie 的 key 和 redis 的 key
        String key = userInfoService.userLogin(username,  password);
        System.out.println(key);
        CookieUtil.addCookie("token", key, RedisKeys.TOKEN_CODE.getTime(), response);
        return AjaxResult.SUCCESS;
    }


}
